using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo
{
    public partial class ctrlGetSimpleEntities : UserControl, DemoControl
    {
        public RichTextBox oTextBox;
        private string sCurrentFile;

        public ctrlGetSimpleEntities(RichTextBox oTextBox)
        {
            InitializeComponent();
            this.oTextBox = oTextBox;
            sCurrentFile = null;
            EnableProcessButton();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            //openFileDialog1.InitialDirectory = SalienceDemo.se4Session.GetDataRoot();
            DialogResult result = openFileDialog1.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                lblCDLFile.Text = "CDL File: " + openFileDialog1.FileName;
                sCurrentFile = openFileDialog1.FileName;

                //Move the browse button
                btnBrowse.Left = lblCDLFile.Right + 5;
            }

        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            processText();
        }

        public void processText()
        {
            if (sCurrentFile == null)
            {
                MessageBox.Show("You must choose a CDL file", "GetSimpleEntities()");
                return;
            }

            gridResults.Rows.Clear();

            try
            {
                SalienceDemo.oSalienceSession.PrepareText(this.oTextBox.Text);
                SalienceDemo.oSalienceSession.SetSimpleEntitiesFile(sCurrentFile);
                List<SalienceEntity> oResults = SalienceDemo.oSalienceSession.GetUserDefinedEntities();

                if (oResults.Count > 0)
                {
                    //Put the results in the grid
                    int nCount = 0;
                    foreach (SalienceEntity oEntity in oResults)
                    {
                        gridResults.Rows.Add();
                        gridResults.Rows[nCount].Cells[0].Value = oEntity.sNormalizedForm;
                        gridResults.Rows[nCount].Cells[1].Value = oEntity.sLabel;
                        gridResults.Rows[nCount].Cells[2].Value = oEntity.fSentimentScore;
                        gridResults.Rows[nCount].Cells[3].Value = oEntity.nEvidence;
                        gridResults.Rows[nCount].Cells[4].Value = oEntity.nCount;
                        gridResults.Rows[nCount].Cells[5].Value = oEntity.nFirstPos;
                        gridResults.Rows[nCount].Cells[6].Value = oEntity.nConfident;
                        nCount++;
                    }
                    gridResults.AutoResizeRows();
                }
                else
                {
                    MessageBox.Show("No Entities Available", "GetUserDefinedEntities()");
                }
            }
            catch (SalienceException e)
            {
                MessageBox.Show(e.Message, "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //Clean up
        private void ctrlGetSimpleEntities_VisibleChanged(object sender, EventArgs e)
        {
            lblCDLFile.Text = "CDL File: ";
            btnBrowse.Left = lblCDLFile.Right + 5;
            gridResults.Rows.Clear();
        }

        #region DemoControl interface methods
        public void btnOptions_Click(object sender, EventArgs e)
        {
            ((SalienceDemo)this.Parent).StartOptionsDialog((int)Dialogs.Options.OptionSections.NOTHING);
            EnableProcessButton();
        }

        public void SetFontSize()
        {
            this.gridResults.DefaultCellStyle.Font = new Font(this.oTextBox.Font.FontFamily, Properties.Settings.Default.fFontSize);
            this.gridResults.AutoResizeRows();
        }

        public void EnableProcessButton()
        {
            this.btnProcess.Enabled = (oTextBox.Text.Trim().Length > 0);
        }
        #endregion
    }
}
